﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.Samples.WindowsPhoneCloud.Web.Models
Imports Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure
Imports Microsoft.IdentityModel.Claims
Imports System.ServiceModel.Web
Imports System.ServiceModel.Activation
Imports System.ServiceModel
Imports System.Net
Imports System.Globalization

Namespace Services

    <ServiceBehavior(IncludeExceptionDetailInFaults:=False), AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
    Public Class RegistrationService
        Implements IRegistrationService
        Private ReadOnly userRepository As IUserRepository
        Private ReadOnly userPrivilegesRepository As IUserPrivilegesRepository

        Public Sub New()
            Me.New(New UserTablesServiceContext(), New UserTablesServiceContext())
        End Sub

        <CLSCompliant(False)> _
        Public Sub New(ByVal userRepository As IUserRepository, ByVal userPrivilegesRepository As IUserPrivilegesRepository)
            Me.userRepository = userRepository
            Me.userPrivilegesRepository = userPrivilegesRepository
        End Sub

        Public Function CreateUser(ByVal user As RegistrationUser) As String Implements IRegistrationService.CreateUser
            If (user Is Nothing) OrElse String.IsNullOrEmpty(user.Name) OrElse String.IsNullOrEmpty(user.EMail) Then
                Throw New WebFaultException(Of String)("Invalid user information.", HttpStatusCode.BadRequest)
            End If

            Dim identity = TryCast(HttpContext.Current.User.Identity, IClaimsIdentity)
            Dim nameIdentifier = identity.Claims.Single(Function(c) c.ClaimType = ClaimTypes.NameIdentifier).Value
            Me.userRepository.CreateUser(nameIdentifier, user.Name, user.EMail)
            Me.SetUserDefaultUserPrivileges(nameIdentifier)

            Return "Success"
        End Function

        Public Function CheckUserRegistration() As String Implements IRegistrationService.CheckUserRegistration
            Dim identity = TryCast(HttpContext.Current.User.Identity, IClaimsIdentity)

            Dim nameIdentifier = identity.Claims.Single(Function(c) c.ClaimType = ClaimTypes.NameIdentifier).Value

            Return (Me.userRepository.GetUser(nameIdentifier) IsNot Nothing).ToString()
        End Function

        Private Sub SetUserDefaultUserPrivileges(ByVal userId As String)
            Me.userPrivilegesRepository.AddPrivilegeToUser(userId, PrivilegeConstants.TablesUsagePrivilege)
            Me.userPrivilegesRepository.AddPrivilegeToUser(userId, PrivilegeConstants.BlobsUsagePrivilege)
            Me.userPrivilegesRepository.AddPrivilegeToUser(userId, PrivilegeConstants.QueuesUsagePrivilege)
            Me.userPrivilegesRepository.AddPrivilegeToUser(userId, PrivilegeConstants.SqlUsagePrivilege)
            Me.userPrivilegesRepository.AddPrivilegeToUser(userId, String.Format(CultureInfo.InvariantCulture, "{0}{1}", "SampleData", PrivilegeConstants.TablePrivilegeSuffix))
        End Sub
    End Class
End Namespace
